");
int iTop = strBody.IndexOf("Top 250:");
int iYear = strBody.IndexOf("/Sections/Years/");
if (iYear >= 0)
{
iYear += "/Sections/Years/".Length;
string strYear = strBody.Substring(iYear, 4);
movieDetails.Year = System.Int32.Parse(strYear);
}
if (iDirectedBy >= 0)
movieDetails.Director = ParseAHREFIMDB(strBody, iDirectedBy, url.URL).Trim();
if (iCredits >= 0)
movieDetails.WritingCredits = ParseAHREFIMDB(strBody, iCredits, url.URL).Trim();
if (iGenre >= 0)
movieDetails.Genre = ParseGenresIMDB(strBody, iGenre, url.URL).Trim();
if (iRating >= 0) // and votes
{
iRating += "User Rating:".Length;
iStart = strBody.IndexOf("", iRating);
if (iStart >= 0)
{
iStart += "".Length;
iEnd = strBody.IndexOf("/", iStart);
// set rating
string strRating = strBody.Substring(iStart, iEnd - iStart);
if (strRating != String.Empty)
strRating = strRating.Replace('.', ',');
try
{
movieDetails.Rating = (float)System.Double.Parse(strRating);
if (movieDetails.Rating > 10.0f)
movieDetails.Rating /= 10.0f;
}
catch (Exception)
{
}
if (movieDetails.Rating != 0.0f)
{
// now, votes
movieDetails.Votes = "0";
iStart = strBody.IndexOf("(", iEnd + 2);
if (iStart > 0)
{
iEnd = strBody.IndexOf(" votes)", iStart);
if (iEnd > 0)
{
iStart += "(".Length; // skip the parantese and link before votes
movieDetails.Votes = strBody.Substring(iStart, iEnd - iStart).Trim();
}
}
}
}
}
if (iTop >= 0) // top rated movie :)
{
iTop += "top 250:".Length + 2; // jump space and #
iEnd = strBody.IndexOf("", iTop);
string strTop = strBody.Substring(iTop, iEnd - iTop);
movieDetails.Top250 = System.Int32.Parse(strTop);
}
if (iTagLine >= 0)
{
iTagLine += "Tagline:".Length;
iEnd = strBody.IndexOf("<", iTagLine);
movieDetails.TagLine = strBody.Substring(iTagLine, iEnd - iTagLine).Trim();
movieDetails.TagLine = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.TagLine);
movieDetails.TagLine = HttpUtility.HtmlDecode(movieDetails.TagLine); // Remove HTML entities like ½
}
if (iPlotOutline < 0)
{
if (iPlotSummary > 0)
{
iPlotSummary += "Plot Summary:".Length;
iEnd = strBody.IndexOf("<", iPlotSummary);
movieDetails.PlotOutline = strBody.Substring(iPlotSummary, iEnd - iPlotSummary).Trim();
movieDetails.PlotOutline = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.PlotOutline);
movieDetails.PlotOutline = HttpUtility.HtmlDecode(movieDetails.PlotOutline); // remove HTML entities
}
}
else
{
iPlotOutline += "Plot Outline:".Length;
iEnd = strBody.IndexOf("<", iPlotOutline);
movieDetails.PlotOutline = strBody.Substring(iPlotOutline, iEnd - iPlotOutline).Trim();
movieDetails.PlotOutline = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.PlotOutline);
movieDetails.PlotOutline = HttpUtility.HtmlDecode(movieDetails.PlotOutline); // remove HTML entities
movieDetails.Plot = movieDetails.PlotOutline.Trim();
movieDetails.Plot = HttpUtility.HtmlDecode(movieDetails.Plot); // remove HTML entities
}
if (iImage >= 0)
{
iEnd = strBody.IndexOf("\"", iImage);
movieDetails.ThumbURL = strBody.Substring(iImage, iEnd - iImage).Trim();
}
//plot
if (iPlot >= 0)
{
string strPlotURL = url.URL + "plotsummary";
try
{
string absoluteUri;
string strPlotHTML = GetPage(strPlotURL, "utf-8", out absoluteUri);
if (0 != strPlotHTML.Length)
{
int iPlotStart = strPlotHTML.IndexOf("");
if (iPlotStart >= 0)
{
iPlotStart += "
".Length;
int iPlotEnd = strPlotHTML.IndexOf("", iPlotStart); // ends with for person who wrote it or
if (iPlotEnd < 0) iPlotEnd = strPlotHTML.IndexOf("
", iPlotStart); // for end of paragraph
if (iPlotEnd >= 0)
{
movieDetails.Plot = strPlotHTML.Substring(iPlotStart, iPlotEnd - iPlotStart);
movieDetails.Plot = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.Plot);
movieDetails.Plot = HttpUtility.HtmlDecode(movieDetails.Plot); // remove HTML entities
}
}
}
}
catch (Exception ex)
{
MediaPortal.GUI.Library.Log.Error("exception for imdb lookup of {0} err:{1} stack:{2}", strPlotURL, ex.Message, ex.StackTrace);
}
}
//cast
string RegCastBlock = "";
string RegActorAndRole = "td class=\"nm\">(?.*?)<.*?(?.*?)<";
Match castBlock = Regex.Match(strBody, RegCastBlock);
// These are some fallback methods to find the block with the cast, in case something changes on IMDB, these may work reasonably well anyway...
if (!castBlock.Success)
castBlock = Regex.Match(strBody, @"redited\scast.*? |
");
if (!castBlock.Success)
castBlock = Regex.Match(strBody, @"first\sbilled\sonly.*?");
if (!castBlock.Success)
castBlock = Regex.Match(strBody, @"redited\scast.*?more");
if (!castBlock.Success)
castBlock = Regex.Match(strBody, @"first\sbilled\sonly.*?more");
string strCastBlock = castBlock.Value;
MatchCollection mc = Regex.Matches(strCastBlock, RegActorAndRole);
string strActor = string.Empty;
string strRole = string.Empty;
foreach (Match m in mc)
{
strActor = string.Empty;
strActor = m.Groups["actor"].Value;
strActor = MediaPortal.Util.Utils.stripHTMLtags(strActor).Trim();
strActor = HttpUtility.HtmlDecode(strActor);
strRole = string.Empty;
strRole = m.Groups["role"].Value;
strRole = MediaPortal.Util.Utils.stripHTMLtags(strRole).Trim();
strRole = HttpUtility.HtmlDecode(strRole);
movieDetails.Cast += strActor;
if (strRole != string.Empty)
movieDetails.Cast += " as " + strRole;
movieDetails.Cast += "\n";
}
int iRunTime = strBody.IndexOf("Runtime:");
if (iRunTime > 0)
{
iRunTime += "Runtime:".Length;
string runtime = "";
while (!Char.IsDigit(strBody[iRunTime]) && iRunTime + 1 < strBody.Length)
iRunTime++;
if (iRunTime < strBody.Length)
{
while (Char.IsDigit(strBody[iRunTime]) && iRunTime + 1 < strBody.Length)
{
runtime += strBody[iRunTime];
iRunTime++;
}
try
{
movieDetails.RunTime = Int32.Parse(runtime);
}
catch (Exception) { }
}
}
int mpaa = strBody.IndexOf("MPAA:");
if (mpaa > 0)
{
mpaa += "MPAA:".Length;
int mpaaEnd = strBody.IndexOf("", mpaa);
if (mpaaEnd > 0)
{
movieDetails.MPARating = strBody.Substring(mpaa, mpaaEnd - mpaa);
}
}
return true;
}
catch (Exception ex)
{
MediaPortal.GUI.Library.Log.Error("exception for imdb lookup of {0} err:{1} stack:{2}", url.URL, ex.Message, ex.StackTrace);
}
return false;
}
string MediaPortal.Video.Database.IIMDBScriptGrabber.GetName()
{
return "IMDB grabber ";
}
string MediaPortal.Video.Database.IIMDBScriptGrabber.GetLanguage()
{
return "EN";
}
private string GetPage(string strURL, string strEncode, out string absoluteUri)
{
string strBody = "";
absoluteUri = String.Empty;
Stream ReceiveStream = null;
StreamReader sr = null;
WebResponse result = null;
try
{
// Make the Webrequest
//Log.Info("IMDB: get page:{0}", strURL);
WebRequest req = WebRequest.Create(strURL);
result = req.GetResponse();
ReceiveStream = result.GetResponseStream();
// Encoding: depends on selected page
Encoding encode = System.Text.Encoding.GetEncoding(strEncode);
sr = new StreamReader(ReceiveStream, encode);
strBody = sr.ReadToEnd();
absoluteUri = result.ResponseUri.AbsoluteUri;
}
catch (Exception)
{
//Log.Error("Error retreiving WebPage: {0} Encoding:{1} err:{2} stack:{3}", strURL, strEncode, ex.Message, ex.StackTrace);
}
finally
{
if (sr != null)
{
try
{
sr.Close();
}
catch (Exception)
{
}
}
if (ReceiveStream != null)
{
try
{
ReceiveStream.Close();
}
catch (Exception)
{
}
}
if (result != null)
{
try
{
result.Close();
}
catch (Exception)
{
}
}
}
return strBody;
} // END GetPage()
string ParseAHREFIMDB(string strBody, int iahref, string strURL)
{
int iStart = strBody.IndexOf("